package shayu.product.excepcation;

import io.renren.common.utils.R;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;

/**
 * 集中处理异常
 */
@RestControllerAdvice(basePackages = "shayu.product.controller")
public class ShayuExceptionControllerAdvice {

    @ExceptionHandler(value = Exception.class)
    public R handleVaildException(MethodArgumentNotValidException e){
        HashMap<String, String> map = new HashMap<>();
        BindingResult result = e.getBindingResult();
        result.getFieldErrors().forEach((item)->{
            String message = item.getDefaultMessage();
            String field = item.getField();
            map.put(field,message);
        });
        return R.error();
    }

}
